<?php
use App\Task;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Validator;

/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Route::get('/tasks', function (){
    $tasks = Task::orderBy('created_at','asc')->get();

   return view('tasks',compact('tasks'));
});

Route::post('/task', function (Request $request){
    $validator = Validator::make($request->all(),[
       'name' => 'required|max:255'
    ]);
    if($validator->fails()){
        return redirect('/tasks')
            ->withInput()
            ->withErrors($validator);
    }

    $task = new Task;
    $task->name = $request->name;
    $task->saveOrFail();

    return redirect('/tasks');
});

Route::delete('/task/{id}', function ($id){
    Task::findOrFail($id)->delete();
    return redirect('/tasks');
});
